﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class SparePartApplyConfirmApplication : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialPage();
            }
        }

        private void InitialPage()
        {
            SparePartApplyService service = new SparePartApplyService();
            SparePartApplyForm sparePartApplyForm = service.GetProductDetailForm(long.Parse(Request.QueryString[Constants.QUERYSTR_SPAREPARTAPPLY_ID]));
            productTypeLabel.Text = sparePartApplyForm.ApplicationProduct.ProductType.ProductTypeName;
            serialNumberLabel.Text = sparePartApplyForm.ApplicationProduct.ProductSerialNumber;
            versionLabel.Text = sparePartApplyForm.ApplicationProduct.ProductVersionNumber;
            problemTypeLabel.Text = sparePartApplyForm.ApplicationProduct.ProblemType.Name;
            decriptionLabel.Text = sparePartApplyForm.ApplicationProduct.ProductProblemDescription;
            appearanceLabel.Text = sparePartApplyForm.ApplicationProduct.ProductAppearance;
            repairTypeLabel.Text = sparePartApplyForm.ApplicationProduct.ProductRepairType;
            examineResultLabel.Text = sparePartApplyForm.ApplicationProduct.Remark;
            Reason.Text = sparePartApplyForm.Reason;
            applicant.Text = sparePartApplyForm.User.ContactPerson;

            serialLabel.Text = sparePartApplyForm.ApplicationProduct.ProductSerialNumber;
            rmacodeLabel.Text = sparePartApplyForm.Code;
            hfProductTypeID.Value = sparePartApplyForm.ApplicationProduct.ProductType.ProductTypeID.ToString();
            applicationCodeLabel.Text = sparePartApplyForm.ApplicationProduct.ApplicationForm.ApplicationCode;
        }

        protected void searchRelultGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                (e.Row.FindControl("selectLinkButton") as LinkButton).CommandArgument = e.Row.RowIndex.ToString();
            }
        }

        protected void remarkDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (remarkDropDownList.Text.Equals("其它型号"))
            {
                otherTextBox.Visible = true;
            }
        }

        protected void disAgreeLinkButton_Click(object sender, EventArgs e)
        {
            disagreeModalPopupExtender.Show();
        }

        protected void saveLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                SparePartApplyService service = new SparePartApplyService();
                SparePartApplyForm form = service.GetSparePartApplyFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_SPAREPARTAPPLY_ID]));
                form.DeclineReason = reasonAndSolutinTextBox.Text;
                form.Status = "备件-不同意";
                form.DealWithTime = DateTime.Now;
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                form.ApplicationProduct.Status = "待修";
                service.Update(form);
                Response.Write(string.Format("<script language='JavaScript'> alert( '你拒绝了单号为:" + form.Code + "的备件申请服务');location.href= 'SparePartApplyListConfirmReturn.aspx' </script>"));
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "SparePartApplyConfirmApplication.aspx::saveLinkButton_Click");
                throw ex;
            }
        }

        protected void agreeLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                SparePartApplyService service = new SparePartApplyService();
                SparePartApplyForm form = service.GetSparePartApplyFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_SPAREPARTAPPLY_ID]));
                form.Status = "备件-同意待发货";
                form.DealWithTime = DateTime.Now;
                if (remarkDropDownList.Text.Equals("其它型号"))
                {
                    form.Remark = otherTextBox.Text;
                }
                else
                {
                    form.Remark = remarkDropDownList.Text;
                }

                service.Update(form);
                Response.Write(string.Format("<script language='JavaScript'> alert( '你同意了单号为:" + form.Code + "的备件申请服务');location.href= 'SparePartApplyListConfirmReturn.aspx' </script>"));
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "SparePartApplyConfirmApplication.aspx::agreeLinkButton_Click");
                throw ex;
            }
        }

        protected void searchLinkButton_Click(object sender, EventArgs e)
        {
            searchModalPopupExtender.Show();
            SparePartService service = new SparePartService();
            searchRelultGridView.DataSource = service.GetSparePartByProductTypeID(Convert.ToInt32(hfProductTypeID.Value));
            searchRelultGridView.DataBind();
        }

        protected void selectLinkButton_Click(object sender, EventArgs e)
        {
            SparePartApplyService service = new SparePartApplyService();
            SparePartApplyForm form = service.GetSparePartApplyFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_SPAREPARTAPPLY_ID]));
            form.ApproveType = ((LinkButton)sender).CommandArgument;
            service.Update(form);
        }        
    }
}
